(function (angular) {
  var module = angular.module("depApp", ["tm.pagination"]);
  module.controller("depCtrl", ["$scope", "$http", function ($scope, $http) {
    // 初始化
    $scope.init = function () {
      $scope.id = "";
      $scope.name = "";
      $scope.code = "";
      $scope.useMark = "0";
      $scope.errMsg="";
    };
    $scope.init();
    // 分页参数:
    $scope.page = {
      currentPage: 1,
      totalItems: 0,
      itemsPerPage: 5,
      pagesLength: 5,
      onChange: function () {
        $scope.getDepList($scope.page.currentPage, $scope.page.itemsPerPage)
      }
    };
    $scope.url = "http://127.0.0.1:8080/lx/";
    // 获取用户列表
    $scope.getDepList = function (currPage, size) {
      if (!currPage && currPage != 0) {
        currPage = $scope.page.currentPage;
      }
      currPage -= 1;
      if (!size) {
        size = $scope.page.itemsPerPage;
      }
      var params = {
        page: currPage,
        size: size ? size : $scope.page.itemsPerPage,
      };
      layer.load();
      $http({
        method: "post",
        url: $scope.url + "getDepListPage",
        data: params,
      }).success(function (res) {
        layer.closeAll("loading");
        // console.log(res);
        if (res.code === 0) {
          $scope.page.content = res.data.content;
          $scope.page.totalItems = res.data.totalElements;
          $scope.page.currentPage = currPage + 1;
          $scope.page.itemsPerPage = size;
        }
      })
    };
    // 删除
    $scope.delete = function (id) {
      var cf = confirm("是否删除?");
      if (cf) {
        layer.load();
        $http({
          method: "post",
          url: $scope.url + "deleteDep",
          data: id,
        }).success(function (res) {
          layer.closeAll("loading");
          // console.log(res);
          if (res.code === 0) {
            layer.alert("删除成功");
            $scope.getDepList($scope.page.currentPage, $scope.page.itemsPerPage)
          } else {
            layer.alert("删除失败");
          }
        })
      }
    };
    // 编辑
    $scope.edit = function (id) {
      $('.dep').fadeIn();
      $scope.init();
      if (id) {
        $scope.headName = "编辑部门";
        $scope.id=id;
        layer.load();
        $http({
          method:"post",
          url:$scope.url+"getDepById",
          data:id
        }).success(function (res) {
          layer.closeAll("loading");
          if(res.code==0){
           console.log(res);
            $scope.id = res.data.id;
            $scope.name = res.data.name;
            $scope.code = res.data.code;
            $scope.useMark = res.data.useMark;
          }
        })
      } else {
        $scope.headName = "添加部门";
      }
    };
    //保存
    $scope.submit = function () {
      if ($scope.name==="") {
        $scope.errMsg="部门名称不能为空!";
        return;
      }
      if ($scope.code==="") {
        $scope.errMsg="部门编码不能为空!";
        return;
      }
      if ($scope.useMark==="") {
        $scope.errMsg="状态不能为空!";
        return;
      }
      layer.load();
      $http({
        method: "put",
        url: $scope.url + "saveDepartment",
        data: {
          id: $scope.id,
          name: $scope.name,
          code: $scope.code,
          useMark: $scope.useMark
        }
      }).success(function (res) {
        layer.closeAll("loading");
        if (res.code === 0) {
          $(".dep").fadeOut();
          layer.alert("保存成功");
          $scope.getDepList($scope.page.currentPage, $scope.page.itemsPerPage)
        } else {
          layer.alert("保存失败");
        }
      })
    };
    // 状态改变
    $scope.changeStatus=function (id,useMark) {
      layer.load();
      $http({
        method:"post",
        url:$scope.url+"changeDepMark",
        data:{
          id:id,
          status:useMark
        }
      }).success(function (res) {
        layer.closeAll("loading");
        if(res.code===0){
          console.log("s");
          $scope.getDepList($scope.page.currentPage, $scope.page.itemsPerPage)
        }else {
          console.log("b");
        }
      })
    }
  }])
})(angular);
